<?php
namespace App\Repositories\Seek;

use App\Models\Model;
use App\Models\Seek\FindNotice;
use App\Repositories\BaseRepository;
use App\Exceptions\OrdinaryException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use PhpParser\Node\Expr\Array_;

class FindNoticeRepository extends BaseRepository {

    public function model()
    {
        // TODO: Implement model() method.
        return FindNotice::class;
    }

    /**
     * 提交需求
     *
     * @author zx
     * @date 2019-01-23
     * @param array $data 参数包
     * @param int $uid 用户ID
     * @return Boolean|int
     */
    public function posts(array $data, int $uid)
    {
        $params = [];
        $params [ 'release_user' ] = $uid;
        $params [ 'province' ] = $data [ 'prov' ];
        $params [ 'city' ] = $data [ 'city' ];
        $params [ 'area' ] = $data [ 'area' ];
        $params [ 'title' ] = $data ['title'];
        if(!$data['title']) $params [ 'title' ] = "寻找".$data [ 'name' ];
        $params [ 'name' ] = $data [ 'name' ];
        if ( isset ( $data['sex'] ) ) $params ['sex'] = $data [ 'sex' ];
        if ( isset ( $data['age'] ) ) $params ['age'] = $data [ 'age' ];
        $params [ 'desc' ] = $data [ 'desc' ];
        $params [ 'address' ] = $data [ 'address' ];
        $params [ 'contact_name' ] = $data [ 'contact_name' ];
        $params [ 'contact_mobile' ] = $data [ 'contact_mobile' ];
        $params [ 'contact_qqorwechat' ] = $data [ 'contact_qqorwechat' ];
        $res = $this->create($params);
        if($res){
            return $res->id;
        }
        return false;
    }

    /**
     * 查询用户是否提交过需求
     *
     * @author zx
     * @date 2019-01-26
     * @param int $uid 用户唯一标识
     * @return Model|Boolean
     */
    public function isPost(int $uid)
    {
        try {
            $res = $this->where("release_user", $uid)
                ->whereIn("status", [0, 1])
                ->first();
            return $res->toArray();
        } catch (ModelNotFoundException $exception) {
            return false;
        }
    }

    /* 获取最新的发布列表
     *
     * @author zx
     * @date 2018-01-29
     * @return Model|Array
     */
    public function getNewLists()
    {
        $lists = $this->where("status", 1)
            ->where("is_delete", 0)
            ->orderBy("id", "desc")
            ->limit(6)
            ->get(['id','name']);
        $res = $lists->toArray();
        foreach($lists as $key =>  $_list){
            foreach ($_list->findimages as $k => $image) {
                if ( $image->cover == 0 ) {
                    $res[$key]['image'] = $image->url;
                    break;
                } elseif ($k == 0) {
                    $res[$key]['image'] = $image->url;
                }
            }
        }
        return $res;
    }

    /**
     * 首页寻人启事列表
     *
     * @author zx
     * @date 2019-01-30
     * @return Model|Array
     */
    public function searchNoticeLists()
    {
        $lists = $this->where("status", 1)
            ->where("is_delete", 0)
            ->orderBy("see", "desc")
            ->limit(16)
            ->get(['id', 'name']);
        $res = $lists->toArray();
        foreach($lists as $key =>  $_list){
            foreach ($_list->findimages as $k => $image) {
                if ( $image->cover == 0 ) {
                    $res[$key]['image'] = $image->url;
                    break;
                } elseif ($k == 0) {
                    $res[$key]['image'] = $image->url;
                }
            }
        }
        return $res;
    }

    /**
     * 获取寻人详情
     *
     * @author zx
     * @date 2019-01-31
     * @param int $id 唯一标识
     * @return Model|Array
     */
    public function detail(int $id)
    {
        $res = $this->where("id", $id)->first();
        $lists = $res->toArray();
        if ($lists) $lists['createtime'] = date("Y-m-d H:i", strtotime($lists['created_at']));
        $image = $res->findimages;
        if ($image) $lists['image'] = $image->toArray();
        $clud = $res->clud;
        if ($clud) $lists['clud'] = $clud->toArray();
        $prov = $res->prov;
        if ($prov) $lists['provName'] = $prov->name;
        $city = $res->cityArr;
        if ($city) $lists['cityName'] = $city->name;
        $area = $res->areaArr;
        if ($area) $lists['areaName'] = $area->name;
        $users = $res->users;
        $lists['nickname'] = "系统";
        if ($users) $lists['nickname'] = $users->nickname;
        return $lists;
    }

    public function serialization(array $attributes)
    {
        // TODO: Implement serialization() method.
    }

}